# @file    : ppt_to_pdf
# @time    : 2025/4/14
# @author  : yongpeng.yao
# @desc    :
from pathlib import WindowsPath

from comtypes import client


def ppt_to_pdf_win(input_path, output_path):
    """
    Windows 系统下使用 PowerPoint 转换 PPT 为 PDF
    需要安装 Microsoft Office 和 PowerPoint
    """
    try:
        powerpoint = client.CreateObject("PowerPoint.Application")
        powerpoint.Visible = 1

        # 打开 PPT 文件
        absolute_path = str(WindowsPath(input_path).absolute())
        deck = powerpoint.Presentations.Open(absolute_path)

        # 保存为 PDF
        absolute_path = str(WindowsPath(output_path).absolute())
        deck.SaveAs(absolute_path, 32)  # 32 是 PDF 格式的枚举值
        deck.Close()

        powerpoint.Quit()
        return True
    except Exception as e:
        print(f"转换失败: {e}")
        return False


if __name__ == '__main__':
    input_path_ = "test.pptx"
    output_path_ = "output.pdf"
    if ppt_to_pdf_win(input_path_, output_path_):
        print("转换成功")
    else:
        print("转换失败")
